package com.ct.ecommerce.framework.core.response;

import com.ct.ecommerce.framework.constant.Constants;
import java.io.Serializable;
import lombok.Data;

/**
 * @author changtong
 * @since 2021/6/2
 */
@Data
public class R<T> implements Serializable {

    public static final int SUCCESS = Constants.SUCCESS;

    public static final int FAIL = Constants.FAIL;

    private int code;

    private String msg;

    private T data;

    public static  <T> R<T> ok(){
        return restResult(null,SUCCESS,"成功!");
    }

    public static  <T> R<T> ok(T data){
        return restResult(data,SUCCESS,"成功!");
    }

    public static  <T> R<T> ok(T data,String msg){
        return restResult(data,SUCCESS,msg);
    }

    public static  <T> R<T> fail(){
        return restResult(null,FAIL,"失败！");
    }

    public static  <T> R<T> fail(T data){
        return restResult(data,FAIL,"失败！");
    }

    public static  <T> R<T> fail(String msg){
        return restResult(null,FAIL,msg);
    }

    private static <T> R<T> restResult(T data, int code, String msg) {
        R<T> apiResult = new R<>();
        apiResult.setCode(code);
        apiResult.setMsg(msg);
        apiResult.setData(data);
        return apiResult;
    }

}
